package com.dc.commons.jpa.listeners;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import com.dc.commons.jpa.TicketWapper;
import com.dc.commons.persistence.able.IDAble;
import com.dc.commons.persistence.able.OperatorAble;
import com.dc.commons.persistence.annocation.DefaultStatus;
import com.dc.commons.spring.SpringBeanFactory;
import com.dc.commons.status.DataStatus;
import com.dc.commons.utils.AnnotationUtil;

/*******************************************************************************<P>
 * 文件名: OperatorJpaListener.java <br>
 * 版本: 1.0<br>
 * 描述:<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年3月24日 下午3:17:27<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
public class OperatorJpaListener {

	/**
	 * 新增
	 * @param idAble
	 */
	@PrePersist
	public void prepersist(IDAble idAble){
		if(idAble instanceof OperatorAble){
			OperatorAble operatorAble=(OperatorAble)idAble;
			TicketWapper ticketWapper=SpringBeanFactory.getBean(TicketWapper.class);
			operatorAble.setCreateDate(ticketWapper.getNow());
			operatorAble.setCreateKey(ticketWapper.getMember());
			DataStatus dataStatus=operatorAble.getDataStatus();
			if(dataStatus==null){
				DefaultStatus defaultStatus=AnnotationUtil.getAnnotation(operatorAble.getClass(), DefaultStatus.class);
				if(defaultStatus!=null){
					dataStatus=defaultStatus.dataStatus();
				}
			}
			operatorAble.setDataStatus(dataStatus);
		}
	}
	
	/**
	 * 修改 记录修改信息
	 * @param idAble
	 */
	@PreUpdate
	public void PreUpdate(IDAble idAble){
		if(idAble instanceof OperatorAble){
			OperatorAble operatorAble=(OperatorAble)idAble;
			TicketWapper ticketWapper=SpringBeanFactory.getBean(TicketWapper.class);
			operatorAble.setUpdateDate(ticketWapper.getNow());
			operatorAble.setUpdateKey(ticketWapper.getMember());
		}
	}
}
